//
//  MenuTableViewController.m
//  JHAssetsPickerViewController
//
//  Created by admin on 15/11/16.
//  Copyright © 2015年 mohe. All rights reserved.
//

#import "MenuTableViewController.h"
#import "JHAssetsPhotoPickerViewController.h"
@interface MenuTableViewController ()<JHAssetsPhotoPickerDelegate>

@end

@implementation MenuTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//用户没有开起相册访问权限
- (void)userNotAuthorizedToAccessPhotoData{
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:@"无法访问相册.请在'设置->定位服务'设置为打开状态." preferredStyle:UIAlertControllerStyleAlert];
    [alertController addAction:cancelAction];
    [self presentViewController:alertController animated:YES completion:nil];
    
}
#pragma mark - Table view data source


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
    //如果应用没有申请过权限 先去申请
    if (status == PHAuthorizationStatusNotDetermined) {
        [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
            
            if (status == PHAuthorizationStatusAuthorized) {
                //用户允许应用访问
                [self pushAssetsPickerViewController:indexPath];
            }else{
                [self userNotAuthorizedToAccessPhotoData];
            }
        }];
    }else if(status == PHAuthorizationStatusAuthorized){
        //用户允许应用访问
        [self pushAssetsPickerViewController:indexPath];
    }else{
        [self userNotAuthorizedToAccessPhotoData];
    }
    
}
- (void)pushAssetsPickerViewController:(NSIndexPath *)indexPath
{
    JHAssetsPhotoPickerViewController *vc = [[JHAssetsPhotoPickerViewController alloc]init];
    vc.delegate = self;
    if(indexPath.section == 0){
        vc.setting.maxSelectCount = 8;
    }else if(indexPath.section == 1){
        vc.setting.maxSelectCount = 1;
    }
    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:vc];
    [self presentViewController:nav animated:YES completion:^{}];
}
- (void)photoPicker:(JHAssetsPhotoPickerViewController *)picker didSelecteFiniehed:(NSArray<PHAsset *> *)result
{
    [result enumerateObjectsUsingBlock:^(PHAsset * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSLog(@"%@", obj);
    }];
}
/*
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:<#@"reuseIdentifier"#> forIndexPath:indexPath];
    
    // Configure the cell...
    
    return cell;
}
*/

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
